#!/usr/bin/perl -w
####################################################################################################
#
# Copyright (c) 2002-2008 Apple, Inc
#
# NAME
#     uninstall-devtools -- Meta-script for running the various devtools uninstaller scripts.
#
# SYNOPSIS
#     sudo /Developer/Library/uninstall-devtools --mode=all
#     sudo /Developer/Library/uninstall-devtools --mode=xcodedir
#     sudo /Developer/Library/uninstall-devtools --mode=unixdev
#     sudo /Developer/Library/uninstall-devtools --mode=systemsupport
#
# Where the specified 'mode' value invokes the following devtools uninstaller scripts:
#
#     all:
#         /Library/Developer/Shared/uninstall-devtools
#         /Library/Developer/3.1/uninstall-devtools
#         /Developer/Library/uninstall-developer-folder
#
#     xcodedir:
#         /Developer/Library/uninstall-developer-folder
#
#     unixdev:
#         /Library/Developer/Shared/uninstall-devtools
#
#     systemsupport:
#         /Library/Developer/Shared/uninstall-devtools
#         /Library/Developer/3.1/uninstall-devtools
#
# The default value for 'mode' is 'all'.
#
# DESCRIPTION
#     This command runs the appropriate devtools uninstaller scripts according to the usage
#     specified on the command line.
####################################################################################################

use strict;
use warnings;

use File::Basename;
use Getopt::Long;
use FindBin qw($Bin);

####################################################################################################

my $do_nothing     = 0;
my $verbose        = 0;
my $warning        = 0;
my $debug          = 0;
my $help           = 0;
my $mode           = '';

if (!GetOptions(
    'do-nothing' => \$do_nothing,
    'verbose' => \$verbose,
    'warning' => \$warning,
    'debug' => \$debug,
    'help' => \$help,
    'mode:s' => \$mode,
)) {
    die("Usage: $0 --mode=<all|xcodedir|unixdev|systemsupport>\n");
}

####################################################################################################

if ($help == 1) {
    print("Usage: $0 --mode=<all|xcodedir|unixdev|systemsupport>\n");
print <<"END";
This is a meta-script which invokes one or more of the devtools
uninstaller scripts, depending on which mode you select.

The recognized modes are:
all:
    /Library/Developer/Shared/uninstall-devtools
    /Library/Developer/3.1/uninstall-devtools
    /Developer/Library/uninstall-developer-folder

xcodedir:
    /Developer/Library/uninstall-developer-folder

unixdev:
    /Library/Developer/Shared/uninstall-devtools

systemsupport:
    /Library/Developer/Shared/uninstall-devtools
    /Library/Developer/3.1/uninstall-devtools

The default value for 'mode' is 'all'.
END
    exit(0);
}

####################################################################################################
# Determine if we are authorized to uninstall the devtools packages.
####################################################################################################

$| = 1;
if (($do_nothing == 0) && ($< != 0)) {
    die("ERROR: Must be run with root permissions -- prefix command with 'sudo'.\n");
}

####################################################################################################

my $uninstaller_dir = $Bin;
my $uninstaller_dir_basename = basename($uninstaller_dir);
my $developer_dir = dirname($uninstaller_dir);

####################################################################################################

my @flags = ();
if ($do_nothing == 1) {
    push(@flags,'--do-nothing');
}
if ($verbose == 1) {
    push(@flags,'--verbose');
}
if ($warning == 1) {
    push(@flags,'--warning');
}
if ($debug == 1) {
    push(@flags,'--debug');
}

if (($mode eq '') || ($mode eq 'all')) {
    run_uninstaller_script("/Library/Developer/3.1/uninstall-devtools",\@flags);
    run_uninstaller_script("/Library/Developer/Shared/uninstall-devtools",\@flags);
    run_uninstaller_script("$developer_dir/Library/uninstall-developer-folder",\@flags);
} elsif ($mode eq 'xcodedir') {
    run_uninstaller_script("$developer_dir/Library/uninstall-developer-folder",\@flags);
} elsif ($mode eq 'unixdev') {
    run_uninstaller_script("/Library/Developer/Shared/uninstall-devtools",\@flags);
} elsif ($mode eq 'systemsupport') {
    run_uninstaller_script("/Library/Developer/3.1/uninstall-devtools",\@flags);
    run_uninstaller_script("/Library/Developer/Shared/uninstall-devtools",\@flags);
} else {
    die("Usage: $0 --mode=<all|xcodedir|shared|systemsupport>\n");
}

####################################################################################################

sub run_uninstaller_script {
    my $script = shift;
    my $flagsref = shift;

    if (-x $script) {
        my @args = ();
        push(@args,$script);
        foreach my $flag (@$flagsref) {
            push(@args,$flag);
        }

        system({$args[0]} @args);
    }
}

####################################################################################################
